<%
require "ubus"
require "uloop"
local util = require "luci.util"
uloop.init()
local function get_macStr()
	local cmd="ifconfig eth0|grep eth0|awk \'{print $5}\'"
	  return(string.sub(luci.sys.exec(cmd),1,-2))
end
local conn=ubus.connect() if not  conn then
	error("failed to connect to ubused")    
end
local myzigpara={macaddr=nil, channel=nil, panid=nil, macaddr}
myzigpara.macaddr=get_macStr()
local tpara=conn:call("smarthome","get_coord_para",{})
if type(tpara) == "table" then
	for k,v in pairs(tpara) do
		if k=="channel" then
			myzigpara.channel=v
		end
		 if k=="panId" then
			myzigpara.panid=v
		end
	end
end

local myzigpara= util.t2j(myzigpara)


%>
<%+header%>                                                                    
<div class="ZigbeeContainers">
    <div class="ZigbeeParams">
        <div class="title">
            <span>Zigbee参数</span>
        </div>
        <div class="titLine" style="width: 100%;height: 2px;"></div>
        <table class="ZigbeeLists">
            <tr>
                <th>MAC地址</th>
                <th>信道</th>
                <th>panid</th>
            </tr>
            <tr class="F5ing">
                <td style="color:#b1b1b1;">正在刷新...</td>
                <td></td>
                <td></td>
            </tr>
        </table>
    </div>
</div>

<script type="text/javascript">//<![CDATA[
var iwxhr = new XHR();
var wifidevs = <%=luci.http.write_json(netdevs)%>;
var arptable = <%=luci.http.write_json(arpcache)%>;
var  myzigpara = <%=myzigpara%>
var timer = false;
console.log(myzigpara)
XHR.poll(5, '<%=luci.dispatcher.build_url("admin", "status", "overview")%>',{status:1},
    function(x, info){
		console.log(info.apTable);
		var ZigbeeLists = $('.ZigbeeLists');
		var zigbeeTabs = info.apTable;
		$('.ZigbeeLists').children().children('.hasALists').remove();
        if((zigbeeTabs.length < 1)||(JSON.stringify(zigbeeTabs) == "{}")){
            $('.F5ing').remove();
            var htmls = '<tr class="hasALists"><td style="color:#b1b1b1;">尚无Zigbee参数信息</td><td></td><td></td></tr>';
            ZigbeeLists.append(htmls);
        }else{
            $('.F5ing').remove();
            for(var t = 0;t < zigbeeTabs.length; t++){
                if(zigbeeTabs[t].status == 1){
                    html = '<tr class="hasALists">' +
                        '                <td>' + zigbeeTabs[t].macaddr + '</td>' +
                        '                <td>' + zigbeeTabs[t].channel + '</td>' +
                        '                <td>' + zigbeeTabs[t].panid + '</td></tr>'        
                ZigbeeLists.append(html);
				}                       
            }
        }
	});
</script>
<%+footer%>